package com.fire.web.modules.all.controller;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fire.common.mybatis.mvc.wrapper.EntityWrapper;
import com.fire.web.modules.all.constant.ErrorCodeConstants;
import com.fire.web.modules.all.entity.AjaxResult;
import com.fire.web.modules.all.entity.FireMaintainorModel;
import com.fire.web.modules.all.entity.FireProprietorModel;
import com.fire.web.modules.all.entity.FireSupervisorModel;
import com.fire.web.modules.all.request.AddMaintainorReq;
import com.fire.web.modules.all.request.AddProprietorReq;
import com.fire.web.modules.all.service.*;
import com.fire.web.modules.all.utils.TokenUtil;
import com.fire.web.modules.all.utils.WrapperUtil;
import com.fire.web.modules.sso.service.IOAuthService;
import com.fire.web.security.shiro.realm.UserRealm;
import com.fire.web.utils.PageRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/maintainor")
public class MaintainorController extends CrudController<IFireMaintainorService, FireMaintainorModel> {
	private static final Logger LOGGER = LoggerFactory.getLogger(MaintainorController.class);

	@Autowired
	ITMitErrorCodeService errorCodeService;
	@Autowired
	IFireMaintainorService fireMaintainorService;
	@Autowired
	private IFireSupervisorService iFireSupervisorService;
	@Autowired
	ISequenceService sequenceService;
	@Autowired
	IOAuthService oAuthService;

	/**
	 * 查询列表
	 *
	 * @return
	 */
	@GetMapping("/getMaintainorList")
	public AjaxResult getMaintainorList(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
		AjaxResult ajaxResult;
		try {
			String token = TokenUtil.getToken(request,response);
			UserRealm.Principal principalByAccessToken = oAuthService.getPrincipalByAccessToken(token);
			String id = principalByAccessToken.getId();
			String username = principalByAccessToken.getUsername();

			//遍历参数获得查询条件
			EntityWrapper ew = WrapperUtil.getEw(params);
			//查询
			Page pageBean = null;
			String adminStr = "admin";
			if(adminStr.equals(username)){
				Wrapper supervisorWrapper = new EntityWrapper();
				supervisorWrapper.eq("adminid",id);
				List<FireSupervisorModel> supervisorList = iFireSupervisorService.selectList(supervisorWrapper);
				if(supervisorList.size()!=1){
					return new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
				}
				FireSupervisorModel supervisor = supervisorList.get(0);
				String supervisorid = supervisor.getSupervisorid();
				ew.eq("supervisor",supervisorid);
				pageBean = fireMaintainorService.selectPage(PageRequest.getPage(), ew);
			} else {
				pageBean = fireMaintainorService.selectPage(PageRequest.getPage(), ew.eq("adminid", id));
			}
			ajaxResult = new AjaxResult(ErrorCodeConstants.SUCCESS, "获取成功", pageBean, "获取成功");
		} catch (Exception e) {
			LOGGER.error("MaintainorController.getMaintainorList().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);

		return ajaxResult;
	}
	/**
	 * 增加维保单位
	 *
	 * @return
	 */
	@PostMapping("/addMaintainor")
	@ResponseBody
	public AjaxResult addMaintainor(AddMaintainorReq req,HttpServletRequest request, HttpServletResponse response) {
		LOGGER.info("==================新增维保单位开始==================");
		LOGGER.info(req.toString());
		AjaxResult ajaxResult;
		try {
			String token = TokenUtil.getToken(request,response);
			UserRealm.Principal principalByAccessToken = oAuthService.getPrincipalByAccessToken(token);
			String id = principalByAccessToken.getId();

			ajaxResult = fireMaintainorService.addMaintainor(req,id);
		} catch (Exception e) {
			LOGGER.error("MaintainorController.addMaintainor().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================新增维保单位结束==================");
		return ajaxResult;
	}

	/**
	 * 更新维保单位
	 *
	 * @return
	 */
	@PostMapping("/updateMaintainor")
	@ResponseBody
	public AjaxResult updateMaintainor(AddMaintainorReq req) {
		LOGGER.info("==================更新维保单位开始==================");
		LOGGER.info(req.toString());
		AjaxResult ajaxResult;
		try {
			ajaxResult = fireMaintainorService.updateMaintainor(req);
		} catch (Exception e) {
			LOGGER.error("MaintainorController.updateMaintainor().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================更新维保单位结束==================");
		return ajaxResult;
	}

	/**
	 * 获取维保单位下拉选菜单
	 * @param req
	 * @return
	 */
	@PostMapping("/getMaintainorSelectItem")
	@ResponseBody
	public AjaxResult getMaintainorSelectItem(AddMaintainorReq req) {
		LOGGER.info("==================获取维保单位下拉选菜单开始==================");
		LOGGER.info(req.toString());
		AjaxResult ajaxResult;
		try {
			ajaxResult = fireMaintainorService.getMaintainorSelectItem();
		} catch (Exception e) {
			LOGGER.error("MaintainorController.getMaintainorSelectItem().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================获取维保单位下拉选菜单结束==================");
		return ajaxResult;
	}

}
